import Vue from 'vue'
import Vuex from 'vuex'
import store from '../store'
let vue = Vue.use(Vuex)
export default class monthApi {

  constructor(options) {}

  get(month, type, sucCallback, failedCallback) {
    vue.http.post('/hr/month/report/query?time=' + month + '&type=' + type).then(response => {
      console.log("query month report response" + response);
      if (response.body.result == 1) {
        console.log('query month data' + JSON.stringify(response.body));
        sucCallback(response)
      } else {
        if (failedCallback)
          failedCallback(response)
      }
    }, response => {
      if (failedCallback)
        failedCallback(response)
    })
  }

  export (belong, type, uid) {
    var fName = belong + '.xlsx'
    var url = encodeURI('/hr/month/report/export?fileName=' + encodeURI(fName) +
      "&belongTime=" + encodeURI(belong) + "&type=" + type +
      "&uid=" + uid)
    window.open(url)
  }

  getLastMonth(time) {
    console.log("time:" + time);
    var times = time.split("-");
    var year = Number.parseInt(times[0]);
    var month = Number.parseInt(times[1]);
    if (month == 1) {
      year = year - 1;
      month = 12;
    } else {
      month = month - 1;
    }
    if (month < 10) {
      month = "0" + month;
    }
    return year + "-" + month;
  }

  getDefaultVal() {
    return {
      people: 0,
      females: 0,
      // 职工人数
      fixedEmployee: 0,
      inviteEmployee: 0,
      groupEmployee: 0,
      directEmployee: 0,
      // 文化程度
      graduate: 0,
      undergraduate: 0,
      juniorCollege: 0,
      highSchool: 0,
      juniorMiddleSchool: 0,
      // 工作岗位
      editing: 0,
      administrativeManagement: 0,
      engineering: 0,
      adMarketing: 0,
      worker: 0,
      // 其它从业人员
      retiedWorker: 0,
      threePartWorker: 0,
      partTimeWorker: 0,
      otherWorker: 0,
      // 保留劳动关系的职工
      keepRetired: 0,
      keepIll: 0,
      keepStudy: 0,
      keepChange: 0,
      keepBorrow: 0,
      keepOther: 0,
      // 从业人员变动情况（增加)
      fromSocial: 0,
      fromTransfer: 0,
      student: 0,
      fromInternalOfCompany: 0,
      fromOutOfCompany: 0,
      fromOther: 0,
      // 从业人员变动情况（减少）
      retired: 0,
      interrupt: 0,
      resignation: 0,
      changed: 0,
      out: 0,
      fired: 0,
      toOther: 0,
      departmentId: 0,
      belongTime: this.getThisMonth()
    }
  }

  getThisMonth() {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    month = month < 10 ? "0" + month : month
    var mydate = year.toString() + "-" + month.toString()
    return mydate
  }

  sumAll(arrays) {
    if (arrays && arrays.length > 0) {
      var obj = arrays[0]
      var sumData = {}
      for (var propName in obj) {
        sumData[propName] = 0
        for (var index in arrays) {
          if (arrays[index] && arrays[index][propName]) {
            sumData[propName] = sumData[propName] + arrays[index][propName]
          }
        }
      }
      return sumData;
    }
    return null
  }

  sum(monthdata) {
    var increaseWorkers = monthdata.fromSocial + monthdata.fromTransfer + monthdata.student + monthdata.fromInternalOfCompany + monthdata.fromOutOfCompany + monthdata.fromOther
    var decreaseWorkers = monthdata.retired + monthdata.interrupt + monthdata.resignation + monthdata.changed + monthdata.out + monthdata.fired + monthdata.toOther
    return {
      positions: monthdata.editing + monthdata.administrativeManagement + monthdata.engineering + monthdata.adMarketing + monthdata.worker,
      increaseWorkers: increaseWorkers,
      totalEmployees: monthdata.fixedEmployee + monthdata.inviteEmployee + monthdata.groupEmployee + monthdata.directEmployee,
      otherWorkers: monthdata.retiedWorker + monthdata.threePartWorker + monthdata.partTimeWorker + monthdata.otherWorker,
      totaledus: monthdata.graduate + monthdata.undergraduate + monthdata.juniorCollege + monthdata.highSchool + monthdata.juniorMiddleSchool,
      decreaseWorkers: decreaseWorkers,
      totalkeeps: monthdata.keepRetired + monthdata.keepIll + monthdata.keepStudy + monthdata.keepChange + monthdata.keepBorrow + monthdata.keepOther,
      totalChanges: increaseWorkers + decreaseWorkers,
    }
  }

  add(data, sucCallback, failedCallback) {
    vue.http.post('/hr/month/report/add', data).then(response => {
      console.log("add month report response" + response);
      if (response.body.result == 1) {
        console.log('list category ' + JSON.stringify(response.body));
        sucCallback(response)
      } else {
        failedCallback(response)
      }
    }, response => {
      if (failedCallback)
        failedCallback(response)
    })
  }

  uploadAudit(data, approvalData, sucCallback, failedCallback) {
    let approval = {
      approver: approvalData.approval,
      reason: approvalData.reason,
      type: 5,
      content: JSON.stringify(data)
    }

    vue.http.post('/hr/approval/add', approval).then(response => {
      console.log("add month approval response" + response);
      if (response.body.result == 1) {
        console.log('stat resp ' + JSON.stringify(response.body));
        sucCallback(response)
      } else {
        failedCallback(response)
      }
    }, response => {
      if (failedCallback)
        failedCallback(response)
    })

  }

  getStat(time, sucCallback, failedCallback) {
    vue.http.post('/hr/month/report/stat?time=' + time).then(response => {
      console.log("stat month report response" + response);
      if (response.body.result == 1) {
        console.log('stat resp ' + JSON.stringify(response.body));
        sucCallback(response)
      } else {
        failedCallback(response)
      }
    }, response => {
      if (failedCallback)
        failedCallback(response)
    })
  }

  update(data, sucCallback, failedCallback) {
    vue.http.post('/hr/month/report/update', data).then(response => {
      console.log("update month report response" + response);
      if (response.body.result == 1) {
        console.log('update category ' + JSON.stringify(response.body));
        sucCallback(response)
      } else {
        failedCallback(response)
      }
    }, response => {
      if (failedCallback)
        failedCallback(response)
    })
  }
}